const fs = require("fs");
const { promisify } = require("util");
const path = require("path");

const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);

const dbPath = path.join(__dirname, "../db.json");

// 封装数据读取
exports.getDb = async () => {
	const data = await readFile(dbPath, "utf8");
	return JSON.parse(data);
};

// 封装数据持久化
exports.saveDb = async (db) => {
	const data = JSON.stringify(db, null, "  ");
	await writeFile(dbPath, data);
};
